package com.m4f.business.service;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import com.m4f.business.model.GlobalConfiguration;
import com.m4f.business.persistence.PMF;
import com.m4f.business.service.ifc.IAppConfigurationService;

public class AppConfigurationService implements IAppConfigurationService {
	
	private static final Logger LOGGER = Logger.getLogger(AppConfigurationService.class.getName());
	private GlobalConfiguration defaultConfiguration;
	private GlobalConfiguration globalConfiguration = null;
	
	public AppConfigurationService(GlobalConfiguration defConfiguration) {
		this.defaultConfiguration = defConfiguration;
	}
	
	public GlobalConfiguration getGlobalConfiguration() {
		if(this.globalConfiguration == null) {
			this.loadGlobalConfiguration();
		}
		return this.globalConfiguration;
	}
	
	public void saveGlobalConfiguration(GlobalConfiguration configuration) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
            pm.makePersistent(configuration);
            this.loadGlobalConfiguration();
        } catch(Exception e) {
        	LOGGER.severe("Error in saveGlobalConfiguration " + 
	    			e.getMessage());
        } finally {
            pm.close();
        }
	}
	
	public void loadDefaultConfiguration() {
		this.deleteGlobalConfiguration();
		this.saveGlobalConfiguration(this.defaultConfiguration);
	}
	
	public void deleteGlobalConfiguration() {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(GlobalConfiguration.class);
		List<GlobalConfiguration> results = new ArrayList<GlobalConfiguration>();
		try {
	        results.addAll((List<GlobalConfiguration>)query.execute());
	        for(GlobalConfiguration globalConfiguration : results) {
	        	pm.deletePersistent(globalConfiguration);
	        }
	    } catch(Exception e) {
	    	LOGGER.severe("Error in getAllCompanies: " + 
	    			e.getMessage());
	    } finally {
	        query.closeAll();
	        pm.close();
	    }
	}
	
	/*******************************************************************************
	 * 
	 *								PRIVATE METHODS
	 *
	 *******************************************************************************/
	
	
	private void loadGlobalConfiguration() {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(GlobalConfiguration.class);
		List<GlobalConfiguration> results = new ArrayList<GlobalConfiguration>();
		try {	        
			results.addAll((List<GlobalConfiguration>)query.execute());
	        if(results.size()>0) {
	        	this.globalConfiguration = results.get(0);
	        	this.globalConfiguration.setSICCodeTypes(this.defaultConfiguration.getSICCodeTypes());
	        	this.globalConfiguration.setBestPracticeTypes(this.defaultConfiguration.getBestPracticeTypes());
	        } else {
	        	this.globalConfiguration = this.defaultConfiguration;
	        }
	    } catch(Exception e) {
	    	LOGGER.severe("Error in getAllCompanies: " + 
	    			e.getMessage());
	    } finally {
	        query.closeAll();
	        pm.close();
	    }
	}
}